﻿<?php
	include '../inc/conn.php';
	session_start();
	extract($_POST);
	extract($_GET);
	unset($_POST,$_GET);
	
	$filepath="../upload/up_file";

	if(!isset($_SESSION['logname'])){
		die("<div>User Session is missing</div>");
	}

	if (isset($act))
	{
		if ($act == 'up')
		{	
			if(!file_exists($filepath)) {  
				mkdir($filepath);  
			}
			
			if(!isset($iFiledesc)){
				$iFiledesc="N/A";
			}
			
			$uploaded_file=$_FILES['iFile']['tmp_name'];
			$ori_file_name=$_FILES["iFile"]["name"];
			$move_to_file =$filepath."/".$ori_file_name;
			
			if(file_exists( $move_to_file )){
				die("文件已经存在");
			}

			if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312", $move_to_file)))
			{
				date_default_timezone_set('Asia/Shanghai');
				$dt = date("Y-m-d H:i:s");
				$db = new mysql();
				$sql="INSERT INTO `eng_file` (`f_name`, `f_desc` , `f_path`, `f_owner`, `f_date`) values('".$ori_file_name."','".$iFiledesc."','".substr($move_to_file, 2)."','".$_SESSION['logchnname']."','".$dt."')";
				$db->query($sql);
				echo "<script>alert('Data added!');window.opener.location.reload();window.close();</script>";
			}else{
				die("move_to_file failed");
			}
			
		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
<link rel="stylesheet" href="/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<title>上传文件</title>
</head>
<body class="background">
<div class="container white-background pagecontent">
<form action="add_file.php?act=up" method="post" enctype="multipart/form-data">
<div>
	<span class="input-group-addon">选择上传文件:</span>
	<input type="file" name="iFile" id="iFile">
	<div class="input-group">
		<span class="input-group-addon">文件描述:</span>
		<input type="text" name="iFiledesc" id="iFiledesc" class="form-control">
	</div>
	<input class="btn btn-success btn-primary" name="submit" value="上传" type="submit">
</div>
</form>
</div>
</body>
</html>
